package com.example.concurrentprogramming8016.CountDownLatch;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * @author liyanhao
 * @Date 2023-01-06 20:34
 */
public class CountDownLatchDemo {
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(1);
        for (int i = 0; i < 5; i++) {
            new Thread(() -> {
                try {
                    //准备完毕 运动员都在阻塞等待
                    countDownLatch.await();
                    String parter = "[" + Thread.currentThread().getName() + "]";
                    System.out.println(parter + "开始执行...");
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }

            }).start();
        }

        System.out.println("开始等待2秒");
        TimeUnit.SECONDS.sleep(2);
        System.out.println("等待结束，开始放行");
        countDownLatch.countDown();
    }
}
